package com.example.myapplication.room.database;

import android.content.Context;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import com.example.myapplication.room.dao.StudentDao;
import com.example.myapplication.room.entity.Student;

/**
 * @author sjh
 * @Description
 * @create 2022/3/16 10:27
 */
@Database(entities = {Student.class}, version = 1, exportSchema = false)
public abstract class StudentDatabase extends RoomDatabase {

    //用户只需要操作Dao来实现增删改查，因此必须暴露dao
    public abstract StudentDao getStudentDao();

    // 单例模式
    private static StudentDatabase INSTANCE;

    public static synchronized StudentDatabase getInstance(Context context) {
        if (INSTANCE == null) {
            INSTANCE = Room.databaseBuilder
                    (context.getApplicationContext(), StudentDatabase.class, "student_db")

                    //默认是异步线程，这局可以强制开启主线程来操作数据库（测试可以用，真实环境性不要用）
                    //.allowMainThreadQueries()

                    .build();
        }
        return INSTANCE;

    }

}
